/**
 *
 * @author Alex
 */
public class SortedList {
    
    private Integer[] elements;
    private int size;

    //pre: cap > 0
    //post: an empty SortedList has been constructed,
    //if pre is true, capacity = cap, otherwise capacity = 0
    public SortedList(int cap){
        if(cap > 0){
            elements = new Integer[cap];
            size = 0;
        }
        else{
            elements = new Integer[10];
            size = 0;
        }

    }

    public int get(int i)
    {
        return elements[i];
    }
    //post: returns true iff the list is empty
    public boolean isEmpty(){
        return size == 0;
    }

    //post: returns true iff the list is full
    public boolean isFull(){
        return size == elements.length;
    }

    //post: returns the number of objects in the list
    public int size(){
        return size;
    }

    //post: the size of elements has been doubled,
    //the contents of the list were not changed
    public void doubleCapacity(){
        Integer[] temp = new Integer[elements.length * 2];

        for(int i = 0; i < size; i++){
            temp[i] = elements[i];
        }
        elements = temp;
    }

    //post: el has been added to the list
    public void add(int el){
        if(this.isFull()){
            this.doubleCapacity();
            this.add(el);
        }
        else{
           int index = 0;
           boolean found = false;

            while(!found && index < size){
                if(elements[index].compareTo(el) >= 0){
                    found = true;
                }
                else index++;
            }
            for(int i = size; i > index; i--){
                elements[i] = elements[i-1];
            }
            elements[index] = el;
            size++;
        }
    }

    //post returns the first position (from left to right) where el was found,
    //returns -1 otherwise
    public int indexOf(Integer el){
        boolean found = false;
        int counter = 0;

        while( !found && (counter < size) ){
            if(elements[counter].compareTo(el) == 0)
                found = true;
            else counter++;
        }

        if(found)
            return counter;
        else return -1;
    }

    //post: an instance of el was removed,
    //iff the list contained instances of el
    public void remove(Integer el){
        int index = this.indexOf(el);

        if(index > -1){
            for(int i = index; i < (size - 1); i++ ){
                elements[i] = elements[i+1];
            }
            elements[size] = null;
            size--;
        }
    }

    @Override
    //post: returns a String representation of SortedList
    public String toString(){
        String res = "<SortedList[";

        for(int i = 0; i < size; i++){
            if(i == (size - 1))
                res += elements[i] + "]>";
            else
                res += elements[i] + ",";
        }

        return res;
    }

}